# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter

import os
class SidamingzhuPipeline:
    def process_item(self, item, spider):
        """
        四大名著
            /书名
                /章节名称
        """
        path = '四大名著'
        if not os.path.exists(path):
            os.mkdir(path)
        book_name = item['url']   # 书名
        new_path = os.path.join(path, book_name)

        book_mulu_name = item['book_mulu_name']  # 章节名称
        con = item['con']  # 书的内容
        if not os.path.exists(new_path):
            os.mkdir(new_path)
        # 写入内容
        with open(new_path+'/'+book_mulu_name+'.txt', 'w', encoding='UTF-8') as f:
            f.write(con)
        print(book_mulu_name, '下载完成')
        return item
